package com.loop.unclelend.ui.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.loop.unclelend.R
import com.loop.unclelend.utils.adapter.UncleRecyclerViewAdapter
import com.loop.unclelend.utils.adapter.UncleViewHolderHelper
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/25 14:42
 * @Annotation：贷款申请 明细 适配器
 */
class Adapter_Apply_Details(recyclerView: RecyclerView?) : UncleRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.uncle_item_apply_details_title) {
    override fun setItemChildListener(helper: UncleViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.layout_item) //点击Item
    }

    @SuppressLint("SetTextI18n")
    override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
        val textTitle = helper.getTextView(R.id.txt_title)
        textTitle.text = model.optString("showName")

        val textValue = helper.getTextView(R.id.txt_value)
        textValue.text = model.optString("showDetail")

        val childArray = model.optJSONArray("childList")
        val childList = arrayListOf<JSONObject>()
        if (childArray != null) {
            for (i in 0..<childArray.length()){
                childList.add(childArray.optJSONObject(i))
            }
        }

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        val adapter = ApplyDescAdapter(recyclerView)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.data = childList
    }

    class ApplyDescAdapter(recyclerView: RecyclerView?) : UncleRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.uncle_item_apply_details_desc) {
        @SuppressLint("SetTextI18n")
        override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
            val textTitle = helper.getTextView(R.id.txt_title)
            textTitle.text = model.optString("showName")

            val textDesc = helper.getTextView(R.id.txt_desc)
            textDesc.text = model.optString("showDetail")
        }
    }
}